/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2023-06-09 10:33:53
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-08-07 11:26:48
 * @FilePath: \ship-schedule-ui\src\widgets\demo\visualize-trace\map.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ref } from "vue"
import * as mars2d from "mars2d"
import "leaflet-plugin-trackplayback/dist/leaflet.trackplayback"
import "leaflet-plugin-trackplayback/dist/control.playback.css"
import "leaflet-plugin-trackplayback/dist/control.trackplayback"

import { GetShipPositionsByDay } from "@mars/common/api/modules/trace"

export let map: mars2d.Map // 地图对象
let trackplaybackControl = null
// 事件对象，用于抛出事件给vue
export const eventTarget = new mars2d.BaseClass()

// 初始化当前业务
export function onMounted(mapInstance: mars2d.Map): void {
  map = mapInstance // 记录map
}

// 释放当前业务
export function onUnmounted(): void {
  map = null
  if (trackplaybackControl != null) {
    trackplaybackControl.remove(map)
  }
}
/**
 *
 *
 * @export
 * @param {*} trackdata
 *
 *
the trackpoint obj properties:
lat: Latitude of target
lng: Longitude of target
time: unix timestamp
dir(Optional): Moving direction(0-360 degrees, Clockwise direction), if you don't provide it, the program can auto caculate the target direction
info(Optional): other static information of the target, it's an array of key-value pairs
 *
 *
const Options = {
  // the play options
  clockOptions: {
    // the default speed
    // caculate method: fpstime * Math.pow(2, speed - 1)
    // fpstime is the two frame time difference
    speed: 13,
    // the max speed
    maxSpeed: 65
  },
  // trackPoint options
  trackPointOptions: {
    // whether draw track point
    isDraw: false,
    // whether use canvas to draw it, if false, use leaflet api `L.circleMarker`
    useCanvas: true,
    stroke: false,
    color: '#ef0300',
    fill: true,
    fillColor: '#ef0300',
    opacity: 0.3,
    radius: 4
  },
  // trackLine options
  trackLineOptions: {
    // whether draw track line
    isDraw: false,
    stroke: true,
    color: '#1C54E2',
    weight: 2,
    fill: false,
    fillColor: '#000',
    opacity: 0.3
  },
  // target options
  targetOptions: {
    // whether use image to display target, if false, the program provide a default
    useImg: false,
    // if useImg is true, provide the imgUrl
    imgUrl: '../../static/images/ship.png',
    // the width of target, unit: px
    width: 8,
    // the height of target, unit: px
    height: 18,
    // the stroke color of target, effective when useImg set false
    color: '#00f',
    // the fill color of target, effective when useImg set false
    fillColor: '#9FD12D'
  }
}
 */
export function setDataSource(trackdata) {

  const trackplayback = mars2d.L.trackplayback(trackdata, map, {
    clockOptions: {
      speed: 13,
      maxSpeed: 65
    },
    trackPointOptions: {
      // whether draw track point
      isDraw: false,
      // whether use canvas to draw it, if false, use leaflet api `L.circleMarker`
      useCanvas: false,
      stroke: false,
      color: "#ef0300",
      fill: false,
      fillColor: "#ef0300",
      opacity: 0.3,
      radius: 1
    },
    trackLineOptions: {
      // whether draw track line
      isDraw: false,
      stroke: true,
      color: "#1C54E2",
      weight: 2,
      fill: false,
      fillColor: "#000",
      opacity: 0.3
    },
    targetOptions: {
      // whether use image to display target, if false, the program provide a default
      // useImg: false,
      // // if useImg is true, provide the imgUrl
      // imgUrl: '../../static/images/ship.png',
      useImg: true,
      imgUrl: "./img/common/ship_yellow.png",
      // the width of target, unit: px
      width: 8,
      // the height of target, unit: px
      height: 18,
      // the stroke color of target, effective when useImg set false
      color: "#00f",
      // the fill color of target, effective when useImg set false
      fillColor: "#9FD12D"
    }
  })
  if (trackplaybackControl != null) {
    trackplaybackControl.remove(map)
  }
  trackplaybackControl = mars2d.L.trackplaybackcontrol(trackplayback, {
    position: "topleft"
  })
  trackplaybackControl.addTo(map)
}
export function dataProcess(data) {
  debugger
  const map = {}
  const dest = []
  for (let i = 0; i < data.length; i++) {
    const ai = data[i]
    if (!map[ai.Name]) {
      dest.push({
        name: ai.Name,
        item: [ai],

        lng: 133.78486666666666,
        lat: 34.34605,
        time: 1502140341,
        dir: 61.8,
        heading: 62,
        info: []
      })
      map[ai.Name] = ai
    } else {
      for (let j = 0; j < dest.length; j++) {
        const dj = dest[j]
        if (dj.Name === ai.Name) {
          dj.item.push(ai)
          break
        }
      }
    }
  }
  console.log(dest)
}
/**
 *
      List<Ship_Trace> listData = ShipTrace.Instance.GetTraceListByTime(startDate, endDate).OrderBy(e => e.Time).ToList();
      var shipList = listData.Select(ele => new
      {
        lng = ele.Lat,
        lon = ele.Lon,
        time = ele.Time.Value.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
        dir = new Random().Next(0, 360),
        heading = new Random().Next(0, 360),
        info = ele.Name,
        Name = ele.Name
      }).GroupBy(ele => ele.Name);
 */


export function showTrackPlay(nowTime: string) {
  GetShipPositionsByDay({
    nowTime: nowTime
  }).then((res) => {
    if (res.ResultType === "Success") {
      console.log(res.Data)
      const trackdata = res.Data
      const trackplayback = mars2d.L.trackplayback(trackdata, map, {
        clockOptions: {
          speed: 10,
          maxSpeed: 30
        },
        trackPointOptions: {
          useCanvas: false
        },
        targetOptions: {
          useImg: true,
          imgUrl: "./img/common/ship_yellow.png"
        }
      })
      if (trackplaybackControl != null) {
        trackplaybackControl.remove(map)
      }
      trackplaybackControl = mars2d.L.trackplaybackcontrol(trackplayback, {
        position: "topleft"
      })
      trackplaybackControl.addTo(map)
    } else {
      // $message(res.msg)
    }
  })
}
